<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\UserSearchHistoryModel;
use think\App;

class UserSearchHistory extends Base {

    private $userSearchHistoryModel;

    private $req;

    private $uid;

    private $type;

    private $page;

    private $limit = 30;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->uid = $this->isLogin();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->type = isset($this->req['type']) ? $this->req['type'] : 1;

        $this->userSearchHistoryModel = new UserSearchHistoryModel();
    }



    public function getList(){

        if(!$this->uid){
            return $this->succData();
        }

        $where=[];
        $where['uid'] = $this->uid;
        $where['type'] = $this->type;
        $where['user_search_status'] = 1;

        $list = $this->userSearchHistoryModel->getList($where,$this->page,$this->limit,"created_at","DESC");

        $total = $this->userSearchHistoryModel->getTotal($where);

        return $this->succData($total,$list);
    }





    /*
     * 删除用户指定的搜索记录
     * */

    public function delUserSearchHistory()
    {
        if(!$this->uid){
            return $this->fail("请先登录");
        }

        $where=[];
        $where['uid'] = $this->uid;
        $where['type'] = $this->type;
        $where['word'] = $this->req['word'];

        $res = $this->userSearchHistoryModel->delAll($where);

        return $res ? $this->succ($res,"成功") : $this->fail("失败");
    }



    /*
  * 删除用户指定的搜索记录
  * */

    public function delUserAllSearchHistory()
    {
        if(!$this->uid){
            return $this->fail("请先登录");
        }

        $where=[];
        $where['uid'] = $this->uid;
        $where['type'] = $this->type;
        $res = $this->userSearchHistoryModel->delAll($where);
        return $res ? $this->succ($res,"成功") : $this->fail("失败");
    }



}
